package vn.com.fsoft.oopai.fsi.mvc;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import vn.com.fsoft.oopai.fsi.common.Common;
import vn.com.fsoft.oopai.fsi.mvc.Controller.LevelMouseAdapter;
import vn.com.fsoft.oopai.fsi.mvc.Controller.OopaiMouseAdapter;

public class View {
	public JFrame window;
	public Container mainPanel;
	public JPanel tablePanel;
	public JPanel sideBar;
	private int tableSize;
	private int numberOfFlag;
	private JButton confirmButton;
	private JLabel[][] board;
	private JButton[] buttonArray;
	private JLabel numToolBreakerLabel;
	private JLabel numRemainingFlagLabel;

	public View(int tableSize, int numberOfFlag){
		this.tableSize = tableSize;
		this.numberOfFlag = numberOfFlag;
		window = new JFrame("UWS - OOPAI");
		mainPanel = window.getContentPane();
		mainPanel.setLayout(new FlowLayout());
		tablePanel = new JPanel();
		sideBar = new JPanel();

        tablePanel.setLayout(new GridLayout(this.tableSize, this.tableSize));
		sideBar.setLayout(new BoxLayout(sideBar, BoxLayout.Y_AXIS));
		board = new JLabel[this.tableSize][this.tableSize];
        for (int i = 0; i < this.tableSize; i++) {
			for (int j = 0; j < this.tableSize; j++) {
				board[i][j] = new JLabel();
				setCellImage(board[i][j], "images/water.gif");
				board[i][j].setBorder(
						new LineBorder(Color.WHITE, 1));
				tablePanel.add(board[i][j]);
			}
        }
		numToolBreakerLabel = new JLabel("#Tool-Breaker: " + this.numberOfFlag);
		sideBar.add(numToolBreakerLabel);
		numRemainingFlagLabel = new JLabel("#Remaining Flag: " + this.numberOfFlag);
		sideBar.add(numRemainingFlagLabel);
		numRemainingFlagLabel.setVisible(false);
		int numberOfLevel = Common.NUMBER_OF_LEVEL;
		buttonArray = new JButton[numberOfLevel];
		for(int i=1; i<numberOfLevel+1; i++ ){
			buttonArray[i-1] = new JButton("Level "+i);
			sideBar.add(buttonArray[i-1]);
		}
		
		confirmButton = new JButton("Confirm"); 
		confirmButton.setVisible(false);
		sideBar.add(confirmButton);
		
		Dimension d = sideBar.getPreferredSize();
		d.width = 120;
		d.height = 200;
		sideBar.setPreferredSize(d);
		
		mainPanel.add(tablePanel);
		mainPanel.add(sideBar);
		
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setResizable(false);
		window.pack();
		window.setVisible(true);
		
	}
	
	public void setCellImage(JLabel cell, String imageFile){
		Toolkit kit = Toolkit.getDefaultToolkit();
		Image wallImage = kit.getImage(imageFile);
		Icon wallIcon = new ImageIcon(wallImage);
		cell.setIcon(wallIcon);
	}
	
	public void notifyWin(int point){
		JOptionPane.showMessageDialog(this.window, "You won with "+point+ " points");
	}
	
	public void notifyLose(){
		JOptionPane.showMessageDialog(this.window, "You lost");
	}
	
	public void addOppaiMouseActionListener(OopaiMouseAdapter ma){
		board[ma.getX()][ma.getY()].addMouseListener(ma);
	}
	
	public void addConfirmButtonActionListener(MouseAdapter ma){
		confirmButton.addMouseListener(ma);
	}
	
	public void addLevelButtonActionListener(LevelMouseAdapter ma){
		buttonArray[ma.getGameLevel().ordinal()].addMouseListener(ma);
	}
	
	public void setToolBreakerVisible(int x, int y){
		setCellImage(board[x][y],"images/boom.png");
	}
	
	public void resetView(){
		
		for(int i=0; i<this.tableSize; i++)
			for (int j = 0; j < this.tableSize; j++) {
				setCellImage(board[i][j],"images/water.gif");
			}
		if(numRemainingFlagLabel.isVisible()){
			System.out.println("Flag Label is visible"+this.numberOfFlag);
				numRemainingFlagLabel.setText("#Remaining Flag: " + this.numberOfFlag);
			
		}
	}
	
	public void setFlag(int x, int y, int remainingFlag){
		setCellImage(board[x][y],"images/flag.png");
		numRemainingFlagLabel.setText("#Remaining Flag: " + remainingFlag);
	}
	
	public void removeFlag(int x, int y, int remainingFlag){
		/*//TODO for test
		if(status != -1)
			setCellImage(board[x][y],"images/water.gif");
		else 
			setCellImage(board[x][y],"images/boom.png");
			*/
		setCellImage(board[x][y],"images/water.gif");
		numRemainingFlagLabel.setText("#Remaining Flag: " + remainingFlag);
	}
	
	public void setAfterClicked(int x, int y, int status){
		setCellImage(board[x][y],"images/Number-"+status+"-icon.png");
	}
	
	public void addConfirmButton(){
		confirmButton.setVisible(true);
		window.pack();
	}
	
	public void destroyConfirmButton(){
		confirmButton.setVisible(false);
		window.pack();
	}
	
	public void addNumRemainingFlagLabel(){
		numRemainingFlagLabel.setVisible(true);
	}
	
	public void destroyNumRemainingFlagLabel(){
		numRemainingFlagLabel.setVisible(false);
	}
}
